package cn.chenxiejia;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/***
 * @program: maven
 * @ClassName: File5
 * @Description: 判断是否是同一个文件
 * @Author: chenxiejia
 * @Date: 2022/1/18 17:15
 * @version : V1.0
 */
public class File5 {
    public static void main(String[] args) {
        String s1 = "E:\\aa.txt";
        String s2 = "D:\\aa.txt";

        if (isfilesame(s1, s2)) {
            System.out.println("文件相同");
        } else {
            System.out.println("文件不同");
        }
    }

    public static Boolean isfilesame(String s1, String s2) {
        return isfilesame(new File(s1), new File(s2));
    }

    public static Boolean isfilesame(File file1, File file2) {
        boolean f = false;
        if (file1.length() == file2.length()) {
            try (FileInputStream f1 = new FileInputStream(file1); FileInputStream f2 = new FileInputStream(file2)) {
                f = DigestUtils.md5Hex(f1.readAllBytes()).equals(DigestUtils.md5Hex(f2.readAllBytes()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
}
